/*
 * Copyright (C) 2008 Google Inc.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */

package com.daan.android;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;


public class PhotoDbAdapter {

    public static final String KEY_LENGTH = "length";
    public static final String KEY_WEIGHT = "weight";
    public static final String KEY_PHOTODATE = "photodate";
    public static final String KEY_DESCRIPTION = "description";
    public static final String KEY_ROWID = "_id";

    private static final String TAG = "PhotoDbAdapter";
    private DatabaseHelper mDbHelper;
    private SQLiteDatabase mDb;

    /**
     * Database creation sql statement
     */
    private static final String DATABASE_CREATE =
        "create table photos (_id integer primary key autoincrement, "
        + "length float, weight float, photodate text not null, description text);";

    private static final String DATABASE_NAME = "data";
    private static final String DATABASE_TABLE = "photos";
    private static final int DATABASE_VERSION = 2;

    private final Context mCtx;

    private static class DatabaseHelper extends SQLiteOpenHelper {

        DatabaseHelper(Context context) {
            super(context, DATABASE_NAME, null, DATABASE_VERSION);
        }

        @Override
        public void onCreate(SQLiteDatabase db) {

            db.execSQL(DATABASE_CREATE);
        }

        @Override
        public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
            Log.w(TAG, "Upgrading database from version " + oldVersion + " to "
                    + newVersion + ", which will destroy all old data");
            db.execSQL("DROP TABLE IF EXISTS notes");
            onCreate(db);
        }
    }

  
    public PhotoDbAdapter(Context ctx) {
        this.mCtx = ctx;
    }


    public PhotoDbAdapter open() throws SQLException {
        mDbHelper = new DatabaseHelper(mCtx);
        mDb = mDbHelper.getWritableDatabase();
        return this;
    }

    public void close() {
        mDbHelper.close();
    }


   
	    public void createPhoto(float length, float weight, String description) {
	    	
	    	String sqlquery = "INSERT INTO " + DATABASE_TABLE + " (length, weight, photodate, description) values (" + length + ", " + weight + ", datetime('now'), '" + description + "')";
	        
	        mDb.execSQL(sqlquery);
	    }

//    public long createPhoto(String title, String body) {
//        ContentValues initialValues = new ContentValues();
//        initialValues.put(KEY_DESCRIPTION, title);
//        initialValues.put(KEY_PHOTODATE, body);
//
//        return mDb.insert(DATABASE_TABLE, null, initialValues);
//    }

    public boolean deletePhoto(long rowId) {

        return mDb.delete(DATABASE_TABLE, KEY_ROWID + "=" + rowId, null) > 0;
    }

   
    public Cursor fetchAllPhotos() {

        return mDb.query(DATABASE_TABLE, new String[] {KEY_ROWID, KEY_LENGTH,
                KEY_WEIGHT, KEY_PHOTODATE, KEY_DESCRIPTION}, null, null, null, null, null);
    }


    public Cursor fetchPhoto(long rowId) throws SQLException {

        Cursor mCursor =

            mDb.query(true, DATABASE_TABLE, new String[] {KEY_ROWID, KEY_LENGTH,
                    KEY_WEIGHT, KEY_PHOTODATE, KEY_DESCRIPTION}, KEY_ROWID + "=" + rowId, null,
                    null, null, null, null);
        if (mCursor != null) {
            mCursor.moveToFirst();
        }
        return mCursor;

    }
    
    public int lastPhotoId() throws SQLException {
    	
    	int mLastPhoto = 0;
    	int colIndex = 0;

        Cursor mCursor =

        	mDb.query(DATABASE_TABLE, new String[] {KEY_ROWID, KEY_LENGTH,
                    KEY_WEIGHT, KEY_PHOTODATE, KEY_DESCRIPTION}, null, null, null, null, null);
        
        colIndex = mCursor.getColumnIndex(KEY_ROWID);
        
        if (mCursor != null) {
            if (mCursor.moveToLast()) {
            mLastPhoto = mCursor.getInt(colIndex);
            return mLastPhoto;}
        }
        return mLastPhoto;

    }

  
    public void updateNote(long rowId, float length, float weight, String description) {
    	String sqlquery = "UPDATE " + DATABASE_TABLE + " SET " + KEY_LENGTH + " = " + length + " " + KEY_WEIGHT + " = " + weight + " " + KEY_DESCRIPTION + " = '" + description + "' WHERE " + KEY_ROWID + " = " + rowId;
    	
        mDb.execSQL(sqlquery);
    }
}
